package com.lhl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lhl.blog.common.pojo.dataobject.BlogInfo;
import com.lhl.blog.common.pojo.request.AddBlogInfoParam;
import com.lhl.blog.common.pojo.request.UpdateBlogInfoParam;
import com.lhl.blog.common.pojo.response.BlogInfoResponse;
import com.lhl.blog.common.utils.BeanConver;
import com.lhl.blog.model.BlogInfoMapper;
import com.lhl.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogInfoMapper blogMapper;

    /**
     * 获取博客列表
     * @return
     */
    @Override
    public List<BlogInfoResponse> getList() {
        //查询数据
        LambdaQueryWrapper<BlogInfo> queryWrapper = new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getDeleteFlag,0)
                .orderByDesc(BlogInfo::getId);
        List<BlogInfo> blogInfos = blogMapper.selectList(queryWrapper);

        //将上面查询到的结果的类型 List<BlogInfo> 转换为 该方法的返回结果 List<BlogInfoResponse>
        //把一个对象转换成另一个对象使用 map
        List<BlogInfoResponse> collect = blogInfos.stream()
                .map(blogInfo -> BeanConver.tans(blogInfo)).collect(Collectors.toList());

        return collect ;
    }


    /**
     * 获取博客详情
     * @param blogId
     * @return
     */
    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        BlogInfo blogInfo = selectBlogById(blogId);
        return   BeanConver.tans(blogInfo);
    }

    /**
     * 更新博客
     * @param param
     * @return
     */
    @Override
    public Boolean updateBlog(UpdateBlogInfoParam param) {
        BlogInfo blogInfo = BeanConver.tans(param);
        try {
            int result = blogMapper.updateById(blogInfo);
            if (result == 1){
                return true;
            }
        } catch (Exception e) {
            log.error("更新博客失败！");
        }
        return false;
    }

    /**
     * 删除博客
     * @param blogId
     * @return
     */
    @Override
    public Boolean deleteBlog(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        try {
            int result = blogMapper.updateById(blogInfo);
            if (result == 1){
                return true;
            }
        } catch (Exception e) {
            log.error("删除博客失败！");
        }
        return false;
    }

    /**
     * 添加博客
     * @param param
     * @return
     */
    @Override
    public Boolean addBlog(AddBlogInfoParam param) {
        BlogInfo blogInfo = BeanConver.tans(param);
        try {
            int result  = blogMapper.insert(blogInfo);
            if (result == 1){
                return true;
            }
        } catch (Exception e) {
            log.error("添加博客失败");
        }
        return false;
    }

    /**
     * 从数据库获取博客详情
     * @param blogId
     * @return
     */
    public BlogInfo selectBlogById(Integer blogId){
        return blogMapper.selectOne(new LambdaQueryWrapper<BlogInfo>()
                .eq(BlogInfo::getId, blogId)
                .eq(BlogInfo::getDeleteFlag, 0));
    }
}














